package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Coupons;
import org.springblade.modules.app.service.ICouponsService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 优惠券 控制器
 *
 * @author BladeX
 * @since 2020-10-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/coupons")
@Api(value = "优惠券", tags = "优惠券接口")
public class CouponsController extends BladeController {

    private final ICouponsService couponsService;


    ////商家优惠券列表
    @GetMapping("/merchantCouponsList")
    public List<Coupons> merchantCouponsList(Coupons params) {
        BladeUser bladeUser = AuthUtil.getUser();
        LambdaQueryWrapper<Coupons> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.eq(Coupons::getDelflag, "1"));
        queryWrapper.and(wrapper -> wrapper.like(Coupons::getUserid, bladeUser.getUserId()));
        List<Coupons> couponsList = couponsService.list(queryWrapper);
        return couponsList;
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入coupons")
    public R<Coupons> detail(Coupons coupons) {
        Coupons detail = couponsService.getOne(Condition.getQueryWrapper(coupons));
        return R.data(detail);
    }

    /**
     * 分页 优惠券
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入coupons")
    public R<IPage<Coupons>> list(Coupons coupons, Query query) {
        IPage<Coupons> pages = couponsService.page(Condition.getPage(query), Condition.getQueryWrapper(coupons));
        return R.data(pages);
    }

    /**
     * 自定义分页 优惠券
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入coupons")
    public R<IPage<Coupons>> page(Coupons coupons, Query query) {
        IPage<Coupons> pages = couponsService.page(Condition.getPage(query), Condition.getQueryWrapper(coupons));
        return R.data(pages);
    }

    /**
     * 新增 优惠券
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入coupons")
    public R save(@Valid @RequestBody Coupons coupons) {
        return R.status(couponsService.save(coupons));
    }

    /**
     * 修改 优惠券
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入coupons")
    public R update(@Valid @RequestBody Coupons coupons) {
        return R.status(couponsService.updateById(coupons));
    }

    /**
     * 新增或修改 优惠券
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入coupons")
    public R submit(@Valid @RequestBody Coupons coupons) {
        return R.status(couponsService.saveOrUpdate(coupons));
    }


    /**
     * 删除 优惠券
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(couponsService.removeByIds(Func.toLongList(ids)));
    }


}
